<template>
  <div class="index">
    <el-row :gutter="24">
      <el-col :span="12" :offset="6">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="管理员头像">
            <el-upload
                ref='uploadfiles'
                :action="url"
                list-type="picture-card"
                :on-success="susses"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>
          <el-form-item label="管理员姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="管理员昵称" prop="nickName">
            <el-input v-model="ruleForm.nickName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="管理员性别" prop="resource">
            <el-radio-group v-model="ruleForm.sex">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="管理员电话" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="ruleForm.email" type="emil"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
<!--            <el-button @click="resetForm('ruleForm')">重置</el-button>-->
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      url: window.g.baseURL + "/file/upload",
      dialogImageUrl: '',
      dialogVisible: false,
      imageUrl: '',
      value: '',
      ruleForm: {
        img: '',
        name: '',
        password: '',
        checkPass: '',
        nickName: '',
        sex: '',
        phone: '',
        email: '',
        department: ''
      },
      rules: {
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        nickName: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ], sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ], phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
          {min: 11, max: 11, message: '长度为11个字符', trigger: 'blur'}
        ], email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    susses(data) {
      // console.log("url" + data);
      this.ruleForm.img = data;
      this.dialogImageUrl = data;
    },
    open1() {
      this.$notify({
        title: '成功',
        message: '添加成功',
        type: 'success'
      });
    },
    failure() { // 添加失败的弹出
      this.$notify.error({
        title: '错误',
        message: '添加失败'
      });
    },
/*    resetForm(formName) {
      this.$refs[formName].resetFields();
    },*/
    // 图片上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 删除表单数据
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },
    // 提交按钮
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.$http.post("rjbAdmin/registered", JSON.stringify(this.ruleForm), "json")
              .then(date => {
                if(date.data === 1){
                  this.$refs.uploadfiles.clearFiles();
                  this.resetForm('ruleForm');
                  this.open1();
                }else{
                 this.failure();
                }
              })
              .catch(function (error) {
                this.failure()
                console.log(error);
              });
        } else {
          this.failure();
          return false;
        }
      });
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
